package aom.example.security.util;

import bom.example.common.utils.Requestutils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 15:05
 * @since JDK 1.8
 */
@Slf4j
public class ResultUtil {

    /**
     * 私有化构造器
     */
    private ResultUtil(){}

    /**
     * 使用response输出JSON
     * @Author Sans
     * @CreateTime 2019/9/28 11:23
     * @Param  resultMap 数据
     * @Return void
     */
    public static void responseJson(ServletResponse response, Map<String, Object> resultMap){
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            out.println(JSON.toJSONString(resultMap));
        } catch (Exception e) {
            log.error("【JSON输出异常】"+e);
        }finally{
            if(out!=null){
                out.flush();
                out.close();
            }
        }
    }
    /**
     * 返回成功示例
     * @Author Sans
     * @CreateTime 2019/9/28 11:29
     * @Param  resultMap  返回数据MAP
     * @Return Map<String,Object> 返回数据MAP
     */
    public static Map<String, Object> resultSuccess(Map<String, Object> resultMap){
        resultMap.put("message","操作成功");
        resultMap.put("code", 200);
        return resultMap;
    }
    /**
     * 返回失败示例
     * @Author Sans
     * @CreateTime 2019/9/28 11:31
     * @Param  resultMap  返回数据MAP
     * @Return Map<String,Object> 返回数据MAP
     */
    public static Map<String, Object> resultError(Map<String, Object> resultMap){
        resultMap.put("message","操作失败");
        resultMap.put("code",500);
        return resultMap;
    }
    /**
     * 通用示例
     * @Author Sans
     * @CreateTime 2019/9/28 11:35
     * @Param  code 信息码
     * @Param  msg  信息
     * @Return Map<String,Object> 返回数据MAP
     */
    public static Map<String, Object> resultCode(Integer code,String msg){
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("message",msg);
        resultMap.put("code",code);
        return resultMap;
    }

    /**
     * 通用JSON返回示例
     * @param code
     * @param msg
     * @return
     */
    public static JSONObject resultCodeJSON(Integer code, String msg){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        return jsonObject;
    }

    /**
     * 通用返回
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static JSONObject resultObjectJSON(Integer code, String msg, Object data){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        jsonObject.put("data",data);
        return jsonObject;
    }


    public static void render(HttpServletResponse response, String contentType, String text) {
        response.setContentType(contentType);
        setHeader(Requestutils.getHttpServletRequest(),response);
        try {
            response.getWriter().write(text);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 设置通用header头
     *
     * @Title: setHeader
     * @param response
     * @return: void
     */
    public static void setHeader(HttpServletRequest request, HttpServletResponse response) {
        /**安全软件认为*是安全漏洞此处调整*/
        response.setHeader("Access-Control-Allow-Methods", "POST,GET,HEAD,OPTIONS,PATCH,DELETE,PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("access-control-allow-credentials", "false");
        /**安全软件认为*是安全漏洞此处调整*/
        String originHeader=request.getHeader("Origin");
        /**限定来自自身服务端或者来自云服务的域名，不可删除originHeader.contains("jeecms.com")判断，否则云服务功能使用有问题*/
        if (originHeader!=null&&originHeader.contains("jeecms.com")) {
            response.setHeader("Access-Control-Allow-Origin", originHeader);
        }if (originHeader!=null&&(originHeader.contains(request.getServerName()+":"+request.getServerPort()))) {
            response.setHeader("Access-Control-Allow-Origin", "");
        }
    }

    /**
     * 发送html。使用UTF-8编码。
     *
     * @param response
     *            HttpServletResponse
     * @param text
     *            发送的字符串
     */
    public static void renderHtml(HttpServletResponse response, String text) {
        render(response, "text/html;charset=UTF-8", text);
    }

    /**
     * 发送json。使用UTF-8编码。
     *
     * @param response
     *            HttpServletResponse
     * @param text
     *            发送的字符串
     */
    public static void renderJson(HttpServletResponse response, String text) {
        render(response, "application/json;charset=UTF-8", text);
    }

    /**
     * 发送文本。使用UTF-8编码。
     *
     * @param response
     *            HttpServletResponse
     * @param text
     *            发送的字符串
     */
    public static void renderText(HttpServletResponse response, String text) {
        render(response, "text/plain;charset=UTF-8", text);
    }


}
